/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */     
#include "stdio.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
struct Person{
	char* name;
	int age;
	int id;
};

osMailQId mailQ01Handle;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId Task1Handle;
osThreadId myTask02Handle;
osMessageQId myQueue01Handle;
osMutexId myMutex01Handle;
osMutexId myRecursiveMutex01Handle;
osSemaphoreId myBinarySem01Handle;
osSemaphoreId myCountingSem01Handle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
   
/* USER CODE END FunctionPrototypes */

void StartTask1(void const * argument);
void StartTask02(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
       
  /* USER CODE END Init */

  /* Create the mutex(es) */
  /* definition and creation of myMutex01 */
  osMutexDef(myMutex01);
  myMutex01Handle = osMutexCreate(osMutex(myMutex01));

  /* Create the recursive mutex(es) */
  /* definition and creation of myRecursiveMutex01 */
  osMutexDef(myRecursiveMutex01);
  myRecursiveMutex01Handle = osRecursiveMutexCreate(osMutex(myRecursiveMutex01));

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* Create the semaphores(s) */
  /* definition and creation of myBinarySem01 */
  osSemaphoreDef(myBinarySem01);
  myBinarySem01Handle = osSemaphoreCreate(osSemaphore(myBinarySem01), 1);

  /* definition and creation of myCountingSem01 */
  osSemaphoreDef(myCountingSem01);
  myCountingSem01Handle = osSemaphoreCreate(osSemaphore(myCountingSem01), 10);

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* definition and creation of myQueue01 */
  osMessageQDef(myQueue01, 16, uint32_t);
  myQueue01Handle = osMessageCreate(osMessageQ(myQueue01), NULL);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
	

	osMailQDef(mailQ01,15,struct Person);
	mailQ01Handle = osMailCreate(osMailQ(mailQ01),NULL);
	
	
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of Task1 */
  osThreadDef(Task1, StartTask1, osPriorityNormal, 0, 128);
  Task1Handle = osThreadCreate(osThread(Task1), NULL);

  /* definition and creation of myTask02 */
  osThreadDef(myTask02, StartTask02, osPriorityIdle, 0, 128);
  myTask02Handle = osThreadCreate(osThread(myTask02), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}



/* USER CODE BEGIN Header_StartTask1 */
/**
  * @brief  Function implementing the Task1 thread.
  * @param  argument: Not used 
  * @retval None
  */
/* USER CODE END Header_StartTask1 */
void StartTask1(void const * argument)
{
    
    
    

  /* USER CODE BEGIN StartTask1 */
//			static portTickType xlasttime;
//		const portTickType ff = pdMS_TO_TICKS(100000);
//		xlasttime = xTaskGetTickCount();
  /* Infinite loop */
  for(;;)
  {
		printf("start task1----------\n");
		
//    osDelay(1000);
		
		osEvent event = osMailGet(mailQ01Handle,osWaitForever);
		if (event.status == osEventMail)
		{			
			struct Person * pp = (struct Person*)event.value.p;
			printf("name= %s\n",pp->name);
			printf("id= %d\n",pp->id);
			printf("age= %d\n",pp->age);
		}
		
//		osEvent event = osMessageGet(myQueue01Handle,osWaitForever);
//		if (event.status == osEventMessage)
//		{			
//			struct Person * pp = (struct Person*)event.value.p;
//			printf("name= %s\n",pp->name);
//			printf("id= %d\n",pp->id);
//			printf("age= %d\n",pp->age);
//		}
		
//		osEvent event = osMessageGet(myQueue01Handle,osWaitForever);
//		if (event.status == osEventMessage)
//		{
//			printf("data= %d\n",event.value.v);
//		}
		
		
//		if (osRecursiveMutexWait(myRecursiveMutex01Handle,osWaitForever)==osOK){
//			printf("osRecursiveMutexWait 1");
//			if (osRecursiveMutexWait(myRecursiveMutex01Handle,osWaitForever)==osOK){
//				printf("osRecursiveMutexWait 2");
//				osRecursiveMutexRelease(myRecursiveMutex01Handle);
//			}
//			osRecursiveMutexRelease(myRecursiveMutex01Handle);
//		}
			
		
//		if (osMutexWait(myMutex01Handle,osWaitForever)== osOK){
//			printf("osMutexWait start task1------\n");
//			osMutexRelease(myMutex01Handle);
//		}
		
		//osSemaphoreRelease(myBinarySem01Handle);
		
//		if (osSemaphoreRelease(myCountingSem01Handle)==osOK)
//		{
//			int count = osSemaphoreGetCount(myCountingSem01Handle);
//			printf("count: %d\n",count);
//			
//		}

		
		//osDelayUntil(&xlasttime,ff);
//		printf(" task2 state   %d\n",osThreadGetState(myTask02Handle));
//		printf("id %d\n",osThreadGetId());
//		printf("id %d\n",Task1Handle);
//		//osThreadYield();
//		osThreadResume(myTask02Handle);
//		printf(" task2 state   %d\n",osThreadGetState(myTask02Handle));

		//osThreadTerminate(NULL); 

  }
  /* USER CODE END StartTask1 */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void const * argument)
{
  /* USER CODE BEGIN StartTask02 */
  /* Infinite loop */
  for(;;)
  {
		printf("start task2\n");
//		osThreadYield();
    osDelay(500);
		
		
//		osMessagePut(myQueue01Handle,10,0);
		
		
		struct Person pers;
		pers.name = "eleven";
		pers.id = 1;
		pers.age = 18;
		
//		osMessagePut(myQueue01Handle,(uint32_t)&pers,0);
		
		osMailPut(mailQ01Handle,&pers);
		
		
		
		
//		if (osSemaphoreWait(myBinarySem01Handle,osWaitForever) == osOK){
//			printf("get baniry\n");
//		}
		
//		if (osMutexWait(myMutex01Handle,osWaitForever)== osOK){
//			printf("osMutexWait start task2\n");
//			osMutexRelease(myMutex01Handle);
//		}
		
				
//		if (osSemaphoreWait(myCountingSem01Handle,osWaitForever) == osOK){
//		}

		
//		osThreadSuspend(myTask02Handle);
		//printf(" task2 state   %d\n",osThreadGetState(myTask02Handle));
  }
  /* USER CODE END StartTask02 */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
     
/* USER CODE END Application */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
